<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <title>amis admin</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <!--
        <link rel="stylesheet" title="default" href="https://unpkg.com/amis@3.2.0/sdk/sdk.css"/>
        <link rel="stylesheet" href="https://unpkg.com/amis@3.2.0/sdk/helper.css"/>
        <link rel="stylesheet" href="https://unpkg.com/amis@3.2.0/sdk/iconfont.css"/>-->

    <link rel="stylesheet" title="default" href="/wapp/amis6.3.0/sdk.css" />
    <link rel="stylesheet" href="/wapp/amis6.3.0/helper.css" />
    <link rel="stylesheet" href="/wapp/amis6.3.0/iconfont.css" />

    <script src="/wapp/amis6.3.0/sdk.js"></script>
    <!--<script src="https://unpkg.com/vue@2"></script>-->
    <!--<script src="https://unpkg.com/history@4.10.1/umd/history.js"></script>-->


    <script language="JavaScript" src="/wapp/jslib/history.min.js"></script>
    <script language="JavaScript" src="/wapp/jslib/jsonpath.min.js"></script>
    <script language="JavaScript" src="/wapp/jslib/axios.min.js"></script>
    <script language="JavaScript" src="/wapp/jslib/dayjs.min.js"></script>
    <script language="JavaScript" src="/wapp/jslib/lodash.min.js"></script>


    <!-- //其他库的js加载完成-->
    <script language="JavaScript" src="/wapp/coom/static.js"></script>
    <script language="JavaScript" src="/wapp/coom/api.js"></script>


    <style>
        html,
        body,
        .app-wrapper {
            position: relative;
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }
    </style>

    <!--124-->

</head>

<body>
    <div id="root" class="app-wrapper"></div>
    <script>
        // 定义事件总线
        const eventBus = {
            listeners: {},

            // 注册事件监听器
            on(event, callback) {
                if (!this.listeners[event]) {
                    this.listeners[event] = [];
                }
                this.listeners[event].push(callback);
            },

            // 派发事件
            emit(event, data) {
                if (this.listeners[event]) {
                    this.listeners[event].forEach((callback) => {
                        callback(data);
                    });
                }
            }
        };

        // 注册登录状态检查的监听器
        eventBus.on(Static.eventBusOn.loginFailure.eventid, Static.eventBusOn.loginFailure.handle);
        eventBus.on(Static.eventBusOn.refreshToken.eventid, Static.eventBusOn.refreshToken.handle);


        // 定时检查登录状态
        setInterval(() => {
            checkLoginStatusFailure();
        }, 5000); // 每5秒检查一次登录状态


        function checkLoginStatusFailure() {
            console.log('检查token开始>>>')

            eventBus.emit(PubF.checkLoginStatus());

            console.log('检查token结束<<<')
            eventBus.emit('');

        }
        //加载页面检查token
        console.log('加载页面检查token>>>>>>>>')
        checkLoginStatusFailure();


        (function () {
            let amis = amisRequire('amis/embed');
            const match = amisRequire('path-to-regexp').match;

            // 如果想用 browserHistory 请切换下这处代码, 其他不用变
            // const history = History.createBrowserHistory();
            const history = History.createHashHistory();

            const app = {
                type: 'app',
                brandName: 'Admin_home', //app名称  todo
                logo: '/wapp/pic/logo/logo.png', //品牌logo todo
                header: {
                    type: 'tpl',
                    inline: false,
                    className: 'w-full',
                    tpl: '<div class="flex justify-between"><div>顶部区域左侧</div><div>顶部区域右侧</div></div>'
                },
                footer: '<div class="p-2 text-center bg-light">底部区域</div>',
                // asideBefore: '<div class="p-2 text-center">菜单前面区域</div>',
                // asideAfter: '<div class="p-2 text-center">菜单后面区域</div>',
                api: './site.json',//初始化app
            };

            function normalizeLink(to, location = history.location) {
                to = to || '';

                if (to && to[0] === '#') {
                    to = location.pathname + location.search + to;
                } else if (to && to[0] === '?') {
                    to = location.pathname + to;
                }

                const idx = to.indexOf('?');
                const idx2 = to.indexOf('#');
                let pathname = ~idx ? to.substring(0, idx) : ~idx2 ? to.substring(0, idx2) : to;
                let search = ~idx ? to.substring(idx, ~idx2 ? idx2 : undefined) : '';
                let hash = ~idx2 ? to.substring(idx2) : location.hash;

                if (!pathname) {
                    pathname = location.pathname;
                } else if (pathname[0] != '/' && !/^https?\:\/\//.test(pathname)) {
                    let relativeBase = location.pathname;
                    const paths = relativeBase.split('/');
                    paths.pop();
                    let m;
                    while ((m = /^\.\.?\//.exec(pathname))) {
                        if (m[0] === '../') {
                            paths.pop();
                        }
                        pathname = pathname.substring(m[0].length);
                    }
                    pathname = paths.concat(pathname).join('/');
                }

                return pathname + search + hash;
            }

            function isCurrentUrl(to, ctx) {
                if (!to) {
                    return false;
                }
                const pathname = history.location.pathname;
                const link = normalizeLink(to, {
                    ...location,
                    pathname,
                    hash: ''
                });

                if (!~link.indexOf('http') && ~link.indexOf(':')) {
                    let strict = ctx && ctx.strict;
                    return match(link, {
                        decode: decodeURIComponent,
                        strict: typeof strict !== 'undefined' ? strict : true
                    })(pathname);
                }

                return decodeURI(pathname) === link;
            }

            //amis实例化
            let amisInstance = amis.embed(
                '#root',
                app,
                {
                    location: history.location,
                    data: {
                        // 全局数据，是受控的数据
                    },
                    context: {
                        // 全局上下文数据, 非受控的数据，无论哪一层都能获取到，包括弹窗自定义数据映射后都能获取到。
                        // 可以用来放一下全局配置等。比如 API_HOST, 这样页面配置里面可以通过 ${API_HOST} 来获取到。
                        API_HOST: 'https://3xsw4ap8wah59.cfc-execute.bj.baidubce.com'
                    }
                },
                {
                    //请求前置处理
                    requestAdaptor: function (api) {
                        // 可以在这里对 api 进行进一步的自定义处理
                        // 通常这里可以做一些日志记录、修改请求参数等操作
                        // 可以在这里添加自定义逻辑来修改 headers
                        const tokenstr = localStorage.getItem('token'); // 假设 token 存储在 localStorage 中

                        let tokenS; //短token
                        if (!_.isEmpty(tokenstr)) {
                            //alert(tokenstr)
                            let tokenArr = JSON.parse(tokenstr);
                            //alert(tokens[0].jwtToken);
                            tokenS = tokenArr[0].jwtToken;
                        }
                        //alert(tokenS);

                        let headers = api['headers'];
                        if (!_.isEmpty(headers)) {
                            headers['token'] = tokenS;
                            //headers['token']="eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJKV1QiLCJ0b2tlbk5hbWUiOiJUb2tlblMiLCJzdWIiOiJ7XCJpZFwiOjEsXCJ1c2VybmFtZVwiOlwidHR0dFwiLFwiZW1haWxcIjpcIjEyM0BxcS5jb21cIixcInBhc3N3b3JkSGFzaFwiOlwiMTIzXCIsXCJwZXJtaXNzaW9uc1wiOltcIlwvdXNlcnNcL3JlZ2lzdGVyXCIsXCJcL3Blcm1pc3Npb25cL3JlZnJlc2hQcmVBdXRob3JpemVUb1Blcm1pc3Npb25cIixcIlwvcGVybWlzc2lvblwvcXVlcnlhbGxcIixcIlwvcGVybWlzc2lvblwvcXVlcnlhbGxkdG9cIl19IiwiZXhwIjoxNzI0MDYyOTMzLCJpYXQiOjE3MjQwNTkzMzMsInRva2VuR3JvdXAiOjE3MjQwNTkzMzN9.i639ckfZERUW5aMftBn4Q_VBBVPC2ZtM6TlKsGZvRRf924sqbwqXxYDoA1QzaIQcJbCskq7Rc4x0fS9nxdQbCW5E7G_6WF7NfgnXdgKLAGRwA-pZFClyHaZ4pCqXDBpCQqnZ2QD4emV0w91Afz2ey5LNbBnp3GdL5i3ObMWSjB-W8xlrR1ObgIG_KinNcb-kfPgjXs0wVkBkueZqDP2dB-QoP3QZdDkyjWMcwy-sT5H0aua1GZ2noc8w7LAEUPCfHtNe7T3r4oFSg3OtG-Bi2CYjU0HtGKu7gmss4xTxXf0k5BWkx09DpTpO5FVpaJ10ChixDKhGgAtm1HG06_us6g";

                        } else {
                            api['headers']['token'] = tokenS;
                            //api['headers']['token'] ="eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJKV1QiLCJ0b2tlbk5hbWUiOiJUb2tlblMiLCJzdWIiOiJ7XCJpZFwiOjEsXCJ1c2VybmFtZVwiOlwidHR0dFwiLFwiZW1haWxcIjpcIjEyM0BxcS5jb21cIixcInBhc3N3b3JkSGFzaFwiOlwiMTIzXCIsXCJwZXJtaXNzaW9uc1wiOltcIlwvdXNlcnNcL3JlZ2lzdGVyXCIsXCJcL3Blcm1pc3Npb25cL3JlZnJlc2hQcmVBdXRob3JpemVUb1Blcm1pc3Npb25cIixcIlwvcGVybWlzc2lvblwvcXVlcnlhbGxcIixcIlwvcGVybWlzc2lvblwvcXVlcnlhbGxkdG9cIl19IiwiZXhwIjoxNzI0MDYyOTMzLCJpYXQiOjE3MjQwNTkzMzMsInRva2VuR3JvdXAiOjE3MjQwNTkzMzN9.i639ckfZERUW5aMftBn4Q_VBBVPC2ZtM6TlKsGZvRRf924sqbwqXxYDoA1QzaIQcJbCskq7Rc4x0fS9nxdQbCW5E7G_6WF7NfgnXdgKLAGRwA-pZFClyHaZ4pCqXDBpCQqnZ2QD4emV0w91Afz2ey5LNbBnp3GdL5i3ObMWSjB-W8xlrR1ObgIG_KinNcb-kfPgjXs0wVkBkueZqDP2dB-QoP3QZdDkyjWMcwy-sT5H0aua1GZ2noc8w7LAEUPCfHtNe7T3r4oFSg3OtG-Bi2CYjU0HtGKu7gmss4xTxXf0k5BWkx09DpTpO5FVpaJ10ChixDKhGgAtm1HG06_us6g";
                        }

                        return api;
                    },

                    //全局 api 响应适配器。
                    // 另外在 amis 配置项中的 api 也可以配置适配器，针对某个特定接口单独处理。
                    responseAdaptor(api, payload, query, request, response) {

                        //alert(JSON.stringify(request));
                        //alert(JSON.stringify(response));
                        //todo  返回token过期  广播刷新token

                        return payload;
                    },

                    // watchRouteChange: fn => {
                    //   return history.listen(fn);
                    // },
                    updateLocation: (location, replace) => {
                        location = normalizeLink(location);
                        if (location === 'goBack') {
                            return history.goBack();
                        } else if (
                            (!/^https?\:\/\//.test(location) && location === history.location.pathname + history.location.search) ||
                            location === history.location.href
                        ) {
                            // 目标地址和当前地址一样，不处理，免得重复刷新
                            return;
                        } else if (/^https?\:\/\//.test(location) || !history) {
                            return (window.location.href = location);
                        }

                        history[replace ? 'replace' : 'push'](location);
                    },
                    jumpTo: (to, action) => {
                        if (to === 'goBack') {
                            return history.goBack();
                        }

                        to = normalizeLink(to);

                        if (isCurrentUrl(to)) {
                            return;
                        }

                        if (action && action.actionType === 'url') {
                            action.blank === false
                                ? (window.location.href = to)
                                : window.open(to, '_blank');
                            return;
                        } else if (action && action.blank) {
                            window.open(to, '_blank');
                            return;
                        }

                        if (/^https?:\/\//.test(to)) {
                            window.location.href = to;
                        } else if (
                            (!/^https?\:\/\//.test(to) &&
                                to === history.pathname + history.location.search) ||
                            to === history.location.href
                        ) {
                            // do nothing
                        } else {
                            history.push(to);
                        }
                    },
                    isCurrentUrl: isCurrentUrl,
                    theme: 'cxd'
                }
            );

            history.listen(state => {
                amisInstance.updateProps({
                    location: state.location || state
                });
            });
        })();
    </script>
</body>

</html>